package labuladong.开篇;

public class 二分搜索一个数 {
    public static void main(String[] args) {
        System.out.println(binarySearch2(new int[] {1, 2, 3, 4, 5, 5, 6, 7}, 5));
        System.out.println(binarySearch3(new int[] {1, 2, 3, 4, 5, 6, 7, 8}, 4));
        System.out.println(binarySearch4(new int[] {1, 2, 3, 4, 5, 5, 6, 7}, 5));
        System.out.println(binarySearch5(new int[] {1, 2, 3, 4, 5, 5, 6, 7}, 5));
    }

    /**
     * 寻找一个普通的数
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearch(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right-left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        return -1;
    }

    /**
     * 二分查找一个数的的左侧边界 (左闭右开)
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearch2(int[] nums, int target) {
        int left  = 0;
        int right = nums.length - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                right = mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid;
            }
        }
        return nums[left]==target ? left : -1;
    }

    /**
     * 二分查找一个数的的左侧边界 (左闭右闭)
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearch3(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            }
        }
        if (left >= nums.length || nums[left] != target) {
            return -1;
        }
        return nums[left]==target ? left : -1;
    }

    /**
     * 二分查找一个数的的右侧边界 (左闭右开)
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearch4(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                left = mid + 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid;
            }
        }
        if (left < 0 || nums[left-1] != target) {
            return -1;
        }
        return left - 1;
    }

    /**
     * 二分查找一个数的的右侧边界 (左闭右闭)
     * @param nums
     * @param target
     * @return
     */
    public static int binarySearch5(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                left = mid + 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            }
        }
        if(right < 0 || nums[right] != target) {
            return -1;
        }
        return right;
    }
}
